package cn.edu.nju.ws.sview.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashSet;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.nju.ws.sview.LogManagement;
import cn.edu.nju.ws.sview.util.MD5;
import cn.edu.nju.ws.sview.util.URIUtil;
import cn.edu.nju.ws.sview.views.User;
import cn.edu.nju.ws.sview.views.User_validate;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				/*String uid = request.getParameter("uid");
				String pwd = request.getParameter("pwd") ;
				String email = request.getParameter("email") ;
				String displayLang = request.getParameter("display-lang") ;
				String from = request.getParameter("from");
				String[] preferLangs = request.getParameterValues("prefer-langs");
				
				String uid=(String)request.getSession().getAttribute("uid");
				String pwd =(String)request.getSession().getAttribute("pwd");
				String email =(String)request.getSession().getAttribute("email");
				String displayLang =(String)request.getSession().getAttribute("displayLang");
				String from =(String)request.getSession().getAttribute("from");
				String[] preferLangs =(String[])request.getSession().getAttribute("preferLangs");*/
				String code=request.getParameter("code");
				String from=request.getParameter("from");
				String uid=User_validate.getNameByCode(code);
				if(uid==null){
					response.sendRedirect("http://ws.nju.edu.cn/sview");
					return;
				}
				User_validate tmp_user=new User_validate(uid);
				String pwd = tmp_user.getPassword();
				String email = tmp_user.getEmail();
				String displayLang = tmp_user.getDisplayLanguage();
				HashSet<String> langs=null;
				try {
					langs = tmp_user.getPreferedLanguages();
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				User user = new User(uid);
				user.setPassword(pwd) ;
				user.setEmail(email);
				user.setLanguages(langs) ;
				user.setDisplayLang(displayLang);
				user.serialize();
				user.serializePasswordEmail();
				user.serializePreferredLanguages();
		        user.serializeDisplayLanguage();
		        try {
					User_validate.deleteUser(uid);
				} catch (Throwable e) {
					e.printStackTrace();
				}
				request.getSession().setAttribute("user", uid) ;
				request.getSession().setAttribute("display-lang", displayLang) ;

				if (from != null && from.equals("relations")) {
					String langString ="en";
					if(langs.size()==1&&langs.contains("zh")){
						langString = "zh";
					}
					response.sendRedirect("./relations/index.jsp?lang=" + langString);
				} else if (from != null) {
					from  = URLDecoder.decode(from,"utf-8");
					String lang = URIUtil.testParam(from, "lang") ;
					if(!langs.contains(lang)){
						from = URIUtil.removeParam(from, "lang") ;
						String langString ="en";
						if(langs.size()==1&&langs.contains("zh")){
							langString = "zh";
						}
						
						if(from.contains("?")){
							from = from+"&lang="+langString;
						}else{
							from = from+"?lang="+langString;
						}
					}
					response.sendRedirect(from);
				} else {
					String location = "/views/index.jsp";
					String[] preferLangs=null;
					Iterator it=langs.iterator();
					int num=0;
					while(it.hasNext()){ 
						preferLangs[num]=(String)it.next();
						num++;
					}	
					if(preferLangs.length ==1 &&preferLangs[0].equals("zh")){
						location = location+"?lang=zh" ;
					}else{
						location = location+"?lang=" + displayLang;
					}
					response.sendRedirect(location);
				}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
